package org.loy.eclipse.post_debug_perspective;

import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.IStartup;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;

import org.eclipse.debug.core.*;

public class Activator extends AbstractUIPlugin implements IStartup
{
	public Activator() {}

	public void start(BundleContext context) throws Exception
	{
		super.start(context);
		plugin = this;

		ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();  
		launchManager.addLaunchListener(launchesListener);
	}

	public void stop(BundleContext context) throws Exception
	{
		ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();  
		launchManager.removeLaunchListener(launchesListener);

		plugin = null;
		super.stop(context);
	}

	public static Activator getDefault()
	{
		return plugin;
	}

	public static ImageDescriptor getImageDescriptor(String path)
	{
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	@SuppressWarnings("deprecation") // TODO remove
	@Override
	public void earlyStartup() {
		// TODO fix deprecation issue:
		// This method is seeded to initialize plugin as soon as possible(in order to subscribe for listening debug
		// events).
		// However this is not a good way to do this because:
		//    * User may cancel plugin startup initialization
		//    * Platform.getPlugin method used to initialize plugin is deprecated
		//
		// It would be great to find a way to use lazy initialization to manage subscribing for debug events.
		// I'll leave this as is right now, but this should be fixed as soon as possible.
		// Note that implementation of IStartup and org.eclipse.startup extension point related to this issue and should
		// be removed too. 
		//
		// also see:
		// http://wiki.eclipse.org/FAQ_Can_I_activate_my_plug-in_when_the_workbench_starts%3F
		// http://wiki.eclipse.org/FAQ_When_does_a_plug-in_get_started%3F
		Platform.getPlugin(PLUGIN_ID);
	}

	public static final String PLUGIN_ID = "org.loy.eclipse.post_debug_perspective"; //$NON-NLS-1$

	private static Activator plugin;
	private static LaunchesListener launchesListener = new LaunchesListener();
}
